package com.zxjbyte.yiyi.module.system.service.log.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zxjbyte.yiyi.framework.common.domain.api.PageResult;
import com.zxjbyte.yiyi.framework.mybatis.util.Condition;
import com.zxjbyte.yiyi.module.system.convert.log.OperateLogConvert;
import com.zxjbyte.yiyi.module.system.domain.dto.log.OperateLogAddDTO;
import com.zxjbyte.yiyi.module.system.domain.entity.OperateLogPO;
import com.zxjbyte.yiyi.module.system.domain.response.log.OperateLogPageResponse;
import com.zxjbyte.yiyi.module.system.mapper.log.OperateLogMapper;
import com.zxjbyte.yiyi.module.system.service.log.IOperateLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author zhangxingjia
 * @Date 2023/3/13 18:31
 * @Version: 1.0
 */
@Service
public class OperateLogServiceImpl implements IOperateLogService {

    @Resource
    private OperateLogMapper operateLogMapper;

    @Override
    public PageResult<OperateLogPageResponse> page() {
        IPage<OperateLogPO> page = operateLogMapper.selectPage(Condition.getSimplePage(), Wrappers.query());
        List<OperateLogPageResponse> responseList = OperateLogConvert.INSTANCE.pageConvert(page.getRecords());
        return PageResult.of(responseList, page);
    }

    @Override
    public void add(OperateLogAddDTO addReqDto) {
        OperateLogPO entity = OperateLogConvert.INSTANCE.convert(addReqDto);
        entity.setCreateTime(new Date());
        entity.setParams(StrUtil.sub(entity.getParams(), 0, OperateLogPO.PARAMS_MAX_LENGTH));
        entity.setResultData(StrUtil.sub(entity.getResultData(), 0, OperateLogPO.RESULT_DATA_MAX_LENGTH));
        operateLogMapper.insert(entity);
    }
}
